package com.example.demo.controller;

import com.example.demo.model.BookInfo;
import com.example.demo.model.PageRequest;
import com.example.demo.model.PageResult;
import com.example.demo.model.UserInfo;
import com.example.demo.servicer.BookUservice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/book")
@RestController
@ResponseBody
public class BookController {
    private static final Logger log = LoggerFactory.getLogger(BookController.class);
    @Autowired
    private BookUservice bookUservice;
    @RequestMapping("/booklist")
    public PageResult<BookInfo> booklist(PageRequest pageRequest) {
        System.out.println();
        PageResult<BookInfo> pageResult=bookUservice.getBookList(pageRequest);
        return pageResult;



    }
    @RequestMapping("/updata")
    public boolean updateBookIn(BookInfo bookInfo){
        if(bookInfo==null){
            return false;
        }
        if(!StringUtils.hasLength(bookInfo.getBookName())||!StringUtils.hasLength(bookInfo.getAuthor())){
            return false;
        }

       if( bookUservice.updateBookInfo(bookInfo)==1){
        return true;
    }
    return false;
    }
 @RequestMapping("/getBookInfoById")
    public BookInfo getBookInfoById(Integer bookId){
     log.info("id{}",bookId);
     System.out.println(bookId);
        if(bookId<0){
            return null;
        }
        BookInfo bookInfo=bookUservice.getBookInfoById(bookId);
        return bookInfo;
    }
    @RequestMapping("/insert")
    public boolean insertBookInfo(BookInfo bookInfo){
        if(bookInfo==null){
            return false;
        }
        int reslut=0;
       reslut= bookUservice.insertBookInfo(bookInfo);
       if(reslut==1){
           return true;
       }
       return false;
    }
    @RequestMapping("/delete")
    public boolean deleteBookInfoById(Integer id){
        System.out.println("删除图书的id等于"+id);
        if(id<0){
            return false;
        }
        int reslut=0;
        reslut= bookUservice.deleteBookInfoById(id);
        if(reslut==1){
            return true;
        }
        return false;
    }
  @RequestMapping("/update")
    public boolean updateBookInfoById(BookInfo bookfoin){

      return bookUservice.updateBookInfoById(bookfoin);

  }
    @RequestMapping("/batch")
    public String batch(@RequestParam List<Integer> ids){
        log.info("ids{}",ids);

        int result=0;
        result=bookUservice.batch(ids);
        if(result<=0){
            return "失败";
        }
        return "成功";

    }
}
